package com.ticket.sass.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.mapper.AreaMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.AreaEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2022-07-15
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaEntity> implements BaseService<AreaEntity> {
    public List<AreaEntity> listBy(String parentCode) {
        QueryWrapper<AreaEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_code", parentCode);
        queryWrapper.orderByAsc("code");
        return this.list(queryWrapper);
    }
    
    
    public Map<String, String> getMergerNamesByCodes(List<String> codes) {
        if (codes.size() == 0){
            return new HashMap<>();
        }
        
        // 创建查询条件，筛选 code 在列表中的记录
        QueryWrapper<AreaEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("code", codes);
        queryWrapper.select("code", "merger_name");
        
        // 查询符合条件的所有记录
        List<AreaEntity> areaEntities = list(queryWrapper);
        
        // 将查询结果转换为 Map<code, mergerName>
        return areaEntities.stream()
            .collect(Collectors.toMap(AreaEntity::getCode, AreaEntity::getMergerName));
    }
}
